package com.cipta.lomob.fragments;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import com.cipta.lomob.R;
import com.cipta.lomob.base.BaseFragment;

public class UpdateProfileFragment extends BaseFragment{
	
	public static String FRAGMENT_TAG = "UpdateProfileFragment";
	private ImageView imgBackground, imgAvatar;
	private Button btnUpdateBackground, btnUpdateAvatar, btnSaved, btnSetBday;
	private EditText edtUsername, edtFirstname, edtLastname, edtPassword, 
	edtMobilePhone, edtBday;
	private RadioGroup rgGender;
	private RadioButton rbMale, rbFemale;
	private Spinner spnCountry;
	
	private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_update_profile, container,false);
		setHasOptionsMenu(true);
		getActivity().getActionBar().setTitle("Edit Profile");
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		//initDate();
//		initializeProcess();
//		initializeActions();
	}
	
	@Override
	public void initializeViews(View view) {
		// TODO Auto-generated method stub
		super.initializeViews(view);
		
		imgAvatar = (ImageView)view.findViewById(R.id.img_update_profile_avatar);
		imgBackground = (ImageView)view.findViewById(R.id.img_update_profile_background);
		btnSaved = (Button)view.findViewById(R.id.btn_update_profile_save);
		btnUpdateAvatar = (Button)view.findViewById(R.id.btn_update_profile_avatar);
		btnUpdateBackground = (Button)view.findViewById(R.id.btn_update_profile_background);
		btnSetBday = (Button)view.findViewById(R.id.btn_update_profile_setbday);
		edtBday = (EditText)view.findViewById(R.id.edt_update_profile_bday);
		edtFirstname = (EditText)view.findViewById(R.id.edt_update_profile_firstname);
		edtLastname = (EditText)view.findViewById(R.id.edt_update_profile_lastname);
		edtPassword = (EditText)view.findViewById(R.id.edt_update_profile_password);
		edtMobilePhone = (EditText)view.findViewById(R.id.edt_update_profile_phone);
		edtUsername = (EditText)view.findViewById(R.id.edt_update_profile_username);
		rgGender = (RadioGroup)view.findViewById(R.id.rg_update_profile_gender);
		rbFemale = (RadioButton)view.findViewById(R.id.rb_update_profile_female);
		rbMale = (RadioButton)view.findViewById(R.id.rb_update_profile_male);
		spnCountry = (Spinner)view.findViewById(R.id.spn_update_profile_country);
	}
	
	@Override
	public void initializeProcess() {
		// TODO Auto-generated method stub
		super.initializeProcess();
		
		String[] country = new String[]{
			"Indonesia", "Malaysia", "Singapore", "Vietnam", "Thailand"
		};
		
		ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(getActivity(), 
				android.R.layout.simple_dropdown_item_1line,
				android.R.id.text1,
				country);
		spnCountry.setAdapter(adapterCountry);
	}
	
	@Override
	public void initializeActions() {
		// TODO Auto-generated method stub
		super.initializeActions();
		
		btnSaved.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnSetBday.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DatePickerDialog datePickerDialog =  new DatePickerDialog(getActivity(),
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
				datePickerDialog.show();
			}
		});
		
		btnUpdateAvatar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnUpdateBackground.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void initDate(){
		// get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();

	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };
     
    private void updateDisplay() {
    	String year,month,day;
    	year = mYear+"";
    	if(mMonth + 1 < 10)
    		month = "0" + (mMonth + 1);
    	else
    		month = (mMonth+1) + "";
    	if(mDay < 10)
    		day = "0" + mDay;
    	else
    		day = mDay + "";
                edtBday.setText(
                    new StringBuilder()
                            // Month is 0 based so add 1
                            .append(year).append("-")
                            .append(month).append("-")
                            .append(day));
            }
}
